/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui;

import easybooking.data.Booking;
import easybooking.data.Customer;
import easybooking.data.Flight;
import easybooking.data.Seat;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

/**
 *
 * @author tim
 */
public class BookingDialog extends JDialog {

    private EBInterface ebi;
    private JPanel dialogPanel;
    private JPanel buttonPanel;
    private JButton btnOk;
    private JButton btnCancel;
    private Booking editableBooking;
    private Flight f;
    private Customer c;
    private Seat s;
    private Date bookingDate;
    private JLabel lblFlight;
    private JTextField txfFlight = new JTextField();
    private JButton btnFlightSelect;
    private JLabel lblCustomer;
    private JTextField txfCustomer = new JTextField();
    private JButton btnCustomerSelect;
    private JLabel lblSeat;
    private JTextField txfSeat = new JTextField();
    private JButton btnSeatSelect;
    private JLabel lblBookingDate;
    private JTextField txfBookingDate = new JTextField();

    public BookingDialog(EBInterface ebif) {
        ebi = ebif;
        //generateComponents();
    }
    // Get Text Fields
/**
 * gets the flight of a booking
 * @return the booked flight
 */
    public Flight getFlight() {
        return this.f;
    }
/**
 * gets the customer of a booking
 * @return the customer of the booking
 */
    public Customer getCustomer() {
        return this.c;
    }
/**
 * gets the flight of a booking
 * @return the booked flight
 */
    public Seat getSeat() {
        return this.s;
    }
/**
 * gets the date of a booking
 * @return the date of the booking
 */
    public Date getBookingDate() {
        return this.bookingDate;
    }
/**
 * get the editable booking object
 * @return the booking object which should be edit
 */
    public Booking getEditableBooking() {
        return editableBooking;
    }
/**
 * sets the flight of the booking into the flight text field
 * @param f is the flight object of the booking
 */
    public void setFlight(Flight f) {
        try {
            this.f = f;
            txfFlight.setText("From: " + ebi.getInterfaceClient().findAirportByKey(f.getStartAirportId()).getCity() + " To: "+ ebi.getInterfaceClient().findAirportByKey(f.getDestAirportId()).getCity());
        } catch (RemoteException ex) {
            Logger.getLogger(BookingDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
/**
 * sets the costumer of the booking into the customer text field
 * @param c is the customer object of the booking
 */
    public void setCustomer(Customer c) {
        try {
            this.c = c;
            txfCustomer.setText(c.getFirstname() +" "+ c.getLastname());
        } catch (RemoteException ex) {
            Logger.getLogger(BookingDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
/**
 * sets the seat of the booking into the seat text field
 * @param s is the seat object of the booking
 */
    public void setSeat(Seat s) {
        try {
            this.s = s;
           txfSeat.setText(String.valueOf(s.getRow()) + String.valueOf(s.getSeat()));
        } catch (RemoteException ex) {
            Logger.getLogger(BookingDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
/**
 * sets the date of the booking into the booking date text field
 * @param d is the date object of the booking
 */
    public void setBookingDate(Date d) {
         txfBookingDate.setText(bookingDate.toString());
        this.bookingDate = d;
    }
/**
 * sets the atributs of the booking into the text fields
 * @param b is the booking object which atributs get instert into the text fields
 */
    public void setTextFields(Booking b) {
        try {
            f = ebi.getInterfaceClient().findFlightByKey(b.getFlightId());
            c = ebi.getInterfaceClient().findCustomerByKey(b.getCustomerId());
            s = ebi.getInterfaceClient().findSeatByKey(b.getSeatId());
            txfBookingDate.setText(b.getDateTime().toString());
            txfFlight.setText("From: " + ebi.getInterfaceClient().findAirportByKey(f.getStartAirportId()).getCity() + " To: "+ ebi.getInterfaceClient().findAirportByKey(f.getDestAirportId()).getCity());
            txfCustomer.setText(c.getFirstname() +" "+ c.getLastname());
            txfSeat.setText(String.valueOf(s.getRow()) + String.valueOf(s.getSeat()));
        } catch (RemoteException ex) {
            Logger.getLogger(BookingDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //shows the dialog when needed
       /**
 * calls the generateComponents() method to create and show the BookingDialog
 * @param b is the booking object which should be edit
 */
    public void createAndShowDialog(Booking b) {
        editableBooking = b;
        generateComponents();
        btnOk.setName("editBooking");
        setTextFields(b);
        this.setResizable(true);
        this.setTitle("Edit Plane");
        this.setModal(true); // deaktivates input to other dialogs
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
        this.toFront();
    }
/**
 * calls the generateComponents() method to create and show the BookingDialog
 */
    public void createAndShowDialog() {
        generateComponents();
        btnOk.setName("createBooking");
        this.bookingDate = new Date();
        txfBookingDate.setText(bookingDate.toString());
        this.setResizable(true);
        this.setTitle("Create Booking");
        this.setModal(true); // deaktivates input to other dialogs
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
        this.toFront();
    }

    // Preperes the dialog
         /**
 * Generates all components to create and display the GUI
 */
    private void generateComponents() {

        // Dialog Settings

        dialogPanel = new JPanel();


        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }



        dialogPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        lblBookingDate = new JLabel("Booking Date:");
        addComponent(dialogPanel, gbl, lblBookingDate, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);

        txfBookingDate = new JTextField(20);
        txfBookingDate.setEditable(false);
        addComponent(dialogPanel, gbl, txfBookingDate, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 0, 1, 1, 0, 0);


        lblFlight = new JLabel("Flight:");
        addComponent(dialogPanel, gbl, lblFlight, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 1, 1, 1, 0, 0);
        txfFlight = new JTextField(20);
        txfFlight.setEditable(false);
        addComponent(dialogPanel, gbl, txfFlight, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 1, 1, 1, 0, 0);
        btnFlightSelect = new JButton("Select");
        btnFlightSelect.setActionCommand("BookingDialogFlightSelect");
        btnFlightSelect.addActionListener(ebi);
        addComponent(dialogPanel, gbl, btnFlightSelect, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 2, 1, 1, 1, 0, 0);

        lblCustomer = new JLabel("Customer:");
        addComponent(dialogPanel, gbl, lblCustomer, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 2, 1, 1, 0, 0);
        txfCustomer = new JTextField(20);
        txfCustomer.setEditable(false);
        addComponent(dialogPanel, gbl, txfCustomer, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 2, 1, 1, 0, 0);
        btnCustomerSelect = new JButton("Select");
        btnCustomerSelect.setActionCommand("BookingDialogCustomerSelect");
        btnCustomerSelect.addActionListener(ebi);
        addComponent(dialogPanel, gbl, btnCustomerSelect, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 2, 2, 1, 1, 0, 0);

        lblSeat = new JLabel("Seat:");
        addComponent(dialogPanel, gbl, lblSeat, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 3, 1, 1, 0, 0);
        txfSeat = new JTextField(20);
        txfSeat.setEditable(false);
        addComponent(dialogPanel, gbl, txfSeat, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 3, 1, 1, 0, 0);
        btnSeatSelect = new JButton("Select");
        btnSeatSelect.setActionCommand("BookingDialogSeatSelect");
        btnSeatSelect.addActionListener(ebi);
        addComponent(dialogPanel, gbl, btnSeatSelect, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 2, 3, 1, 1, 0, 0);

        // adds to buttonPanel
        addComponent(dialogPanel, gbl, this.createButtonPanel(), GridBagConstraints.NONE, GridBagConstraints.EAST, 0, 6, 2, 1, 0, 0);

        // sets die dialogPanel as contentPane
        this.setContentPane(dialogPanel);


    }

    // Creates the button panel
    /**
 * Creates the buttons to commit with ok or cancel
 * @return a JPanel with 2 buttons
 */
    private JPanel createButtonPanel() {

        buttonPanel = new JPanel();

        btnOk = new JButton("OK");
        btnCancel = new JButton("Cancel");

        // Sets the buttons the same size
        btnOk.setPreferredSize(btnCancel.getPreferredSize());

        // Registrate action listeners
        btnOk.setActionCommand("bookingDialogOk");
        btnOk.addActionListener(ebi);

        btnCancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        // Adds the buttons to the buttonPanel
        buttonPanel.add(btnOk);
        buttonPanel.add(btnCancel);
        return buttonPanel;
    }
/**
 * defines the layout of the GUI
 * @param cont container to which the component is added
 * @param gbl GridBagLayout object
 * @param c compontent which is added to the container
 * @param fill how to use free space in the cell around the component
 * @param anchor where to fix an align the component
 * @param x x position in the grid layout
 * @param y y position in the grid layout
 * @param width number of columns the element should spread
 * @param hight number of rows the element should spread
 * @param weightx handels the horizontal free space
 * @param weighty handels the vertical free space
 */
    private static void addComponent(Container cont, GridBagLayout gbl, Component c, int fill, int anchor, int x, int y, int width, int hight, double weightx, double weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = fill;
        gbc.anchor = anchor;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = hight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = new Insets(10, 10, 10, 10);
        //gbl.setConstraints(c, gbc);
        cont.add(c, gbc);


    }
}
